﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Reflection;

namespace Krile.Forms.Dialogs.Editor.SearchData
{
    public partial class SearchDataEditor : Form
    {
        private Krile.Kernel.Data.SearchData __selectedData;
        internal Krile.Kernel.Data.SearchData SelectedData
        {
            get
            {
                return __selectedData;
            }
            set
            {
                __selectedData = value;
                excludeMode.Checked = __selectedData.Exclude;
                SetArgumentEditor();
            }
        }

        public SearchDataEditor()
        {
            InitializeComponent();
            __selectedData = null;
        }

        private Krile.Kernel.Control.SDataArgSetUI argUi = null;
        private void SetArgumentEditor()
        {
            if (argUi != null)
            {
                ConfigParentPanel.Controls.Remove(argUi);
                argUi.Dispose();
                argUi = null;
            }
            if (SelectedData != null)
            {
                argConfBox.Enabled = true;
                if ((argUi = SelectedData.GetArgSetUI()) != null)
                {
                    ConfigParentPanel.Controls.Add(argUi);
                    argUi.Location = new Point(0, 0);
                }
            }
            else
                argConfBox.Enabled = false;
        }

        List<Krile.Kernel.Data.SearchData> sdarray;
        private void SearchDataEditor_Load(object sender, EventArgs e)
        {
            sdarray = new List<Krile.Kernel.Data.SearchData>();
            sdarray.AddRange(Core.StatusManager.GetAllSearchData());
            foreach (var sd in sdarray)
                SearchDataList.Items.Add(sd.Name);
            Subsystem.Debugger.Assert(SearchDataList.Items.Count != 0, "Search data not found.");
            if (SelectedData == null)
                SearchDataList.SelectedIndex = 0;
            else
            {
                Krile.Kernel.Data.SearchData buf = SelectedData;
                SearchDataList.Text = SelectedData.Name;
                SelectedData = buf;
            }
        }

        private void SearchDataList_SelectedIndexChanged(object sender, EventArgs e)
        {
            SelectedData = sdarray[SearchDataList.SelectedIndex];
        }

        private void OKBtn_Click(object sender, EventArgs e)
        {
            SelectedData.Exclude = excludeMode.Checked;
            if (argUi != null)
                argUi.Accepted();
        }

        private void CancelBtn_Click(object sender, EventArgs e)
        {
            if (argUi != null)
                argUi.Cancelled();
        }
    }
}
